package ysq.xyg.ticket.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.ticket.dao.TicketProductDao;
import ysq.xyg.ticket.model.po.TicketProduct;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.ticket.model.vo.TicketProductListVo;
import ysq.xyg.ticket.model.vo.TicketProductVo;
import ysq.xyg.ticket.service.TicketProductService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 销售模块-销售产品service实现
 *
 * @author shengQiang yu
 * @date 2019-10-24 10:15:36
 */
@Service("ticketProductService")
public class TicketProductServiceImpl extends BaseServiceImpl<TicketProduct> implements TicketProductService {

    @Resource
    private TicketProductDao ticketProductDao;

    private TicketProduct ticketProduct;

    @Autowired
    public void setBaseDao(TicketProductDao ticketProductDao) {
        this.baseDao = ticketProductDao;
    }


    /**
     * 分页查询
     *
     * @param ticketProductVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @param type 类型：1、默认结果，2、TicketProductListVo结果
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-24 10:15:36
     */
    public ReturnEntity page(TicketProductVo ticketProductVo, int pageSize, int pageNum,String type) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<TicketProduct> list = ticketProductDao.listLeft(ticketProductVo);
        PageInfo<TicketProduct> page=new PageInfo<TicketProduct>(list);
        if("2".equals(type)){
            return new ReturnEntity(changeTicketProductListVo(page.getList()),page.getTotal(),page.isHasNextPage());
        }else{
            return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
        }

    }

    /**
     * 修改
     *
     * @param ticketProductVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-24 10:15:36
     */
    public ReturnEntity addOrUpdate(TicketProductVo ticketProductVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(ticketProductVo.getId())){
            ticketProduct=new TicketProduct(currentUser);
            isUpate=false;
        }else{
            ticketProduct=selectByPrimaryKey(ticketProductVo.getId());
            if(ObjectUtil.isNull(ticketProduct)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            ticketProduct.setUpdateTime(new Date());
            ticketProduct.setUpdateUser(currentUser.getId());
        }
        ticketProduct.setFristShow(ticketProductVo.getFristShow());
        ticketProduct.setRankShow(ticketProductVo.getRankShow());
        ticketProduct.setName(ticketProductVo.getName());
        ticketProduct.setColId(ticketProductVo.getColId());
        ticketProduct.setThumImage(ticketProductVo.getThumImage());
        ticketProduct.setImages(ticketProductVo.getImages());
        ticketProduct.setCostPrice(ticketProductVo.getCostPrice());
        ticketProduct.setCurrentPrice(ticketProductVo.getCurrentPrice());
        ticketProduct.setStars(ticketProductVo.getStars());
        ticketProduct.setScore(ticketProductVo.getScore());
        ticketProduct.setTags(ticketProductVo.getTags());
        ticketProduct.setContent(ticketProductVo.getContent());
        ticketProduct.setSort(ticketProductVo.getSort());
        ticketProduct.setSupplierId(ticketProductVo.getSupplierId());
        ticketProduct.setLatitude(ticketProductVo.getLatitude());
        ticketProduct.setLongitude(ticketProductVo.getLongitude());
        ticketProduct.setProvinceId(ticketProductVo.getProvinceId());
        ticketProduct.setCityId(ticketProductVo.getCityId());
        ticketProduct.setAreaId(ticketProductVo.getAreaId());
        ticketProduct.setAddress(ticketProductVo.getAddress());
        ticketProduct.setShareOneMoney(ticketProductVo.getShareOneMoney());
        ticketProduct.setShareTwoMoney(ticketProductVo.getShareTwoMoney());
        ticketProduct.setShowType(ticketProductVo.getShowType());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(ticketProduct);
        }else{
            num=insert(ticketProduct);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-24 10:15:36
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        TicketProduct ticketProduct=selectByPrimaryKey(id);
        if(ticketProduct!=null){
            ticketProduct.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            ticketProduct.setUpdateTime(new Date());
            ticketProduct.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(ticketProduct);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param ticketProductVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-10-24 10:15:36
     */
    public List<TicketProduct> acceptList(TicketProductVo ticketProductVo){
        Example example = new Example(TicketProduct.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(ticketProductVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",ticketProductVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(ticketProductVo.getShowType())){
            criteria.andEqualTo("showType",ticketProductVo.getShowType());
        }
        if(StringUtil.isNotEmpty(ticketProductVo.getFristShow())){
            criteria.andEqualTo("fristShow",ticketProductVo.getFristShow());
        }
        if(StringUtil.isNotEmpty(ticketProductVo.getRankShow())){
            criteria.andEqualTo("rankShow",ticketProductVo.getRankShow());
        }
        if(StringUtil.isNotEmpty(ticketProductVo.getCityId())){
            criteria.andEqualTo("cityId",ticketProductVo.getCityId());
        }
        if(StringUtil.isNotEmpty(ticketProductVo.getProvinceId())){
            criteria.andEqualTo("provinceId",ticketProductVo.getProvinceId());
        }
        if(StringUtil.isNotEmpty(ticketProductVo.getAreaId())){
            criteria.andEqualTo("areaId",ticketProductVo.getAreaId());
        }
        if(StringUtil.isNotEmpty(ticketProductVo.getColId())){
            criteria.andEqualTo("colId",ticketProductVo.getColId());
        }
        if(StringUtil.isNotEmpty(ticketProductVo.getName())){
            criteria.andLike("name","%"+ticketProductVo.getName()+"%");
        }
        example.setOrderByClause(" sort desc,update_time desc");
        return ticketProductDao.selectByExample(example);
    }
    @Override
    public List<TicketProductListVo> changeTicketProductListVo(List<TicketProduct> list){
        List<TicketProductListVo> ticketProductListVos=new ArrayList<TicketProductListVo>();
        if(list!=null&&list.size()>0){
            for(TicketProduct ticketProduct:list){
                ticketProductListVos.add(new TicketProductListVo(ticketProduct.getId(),ticketProduct.getName(),ticketProduct.getThumImage(),ticketProduct.getCostPrice(),ticketProduct.getCurrentPrice(),ticketProduct.getStars(),ticketProduct.getScore(),ticketProduct.getTags(),ticketProduct.getAreaName()));
            }
        }
        return ticketProductListVos;
    }
}
